<script setup>
import { ElPagination, ElButton } from 'element-plus'
import { onMounted, getCurrentInstance, ref } from 'vue'
import { useRouter } from 'vue-router'
import OrderHeader from '@/components/OrderHeader.vue'
import Loading from '@/components/Loading.vue'
import NoData from '@/components/NoData.vue'

const prefix = import.meta.env.VITE_BASE_URL
const { proxy } = getCurrentInstance()
const list = ref([])
const pageSize = ref(10)
const pageNum = ref(1)
const total = ref(100)
const loading = ref(false)
const busy = ref(false)    // 滚动加载是否触发
const showNextPage = ref(false)
const router = useRouter()
onMounted(() => getOrderList())
const getOrderList = () => {
  loading.value = true
  busy.value = true
  proxy.axios.get('/orders', {
    // params: {
    //   pageNum: pageNum.value
    // }
  }).then(res => {
    loading.value = false
    list.value = list.value.concat(res.list)
    busy.value = false
    // total.value = res.total
    showNextPage.value = res.hasNextPage
  }).catch(() => {
    loading.value = false
  })
}
// 专门给scrollmore()使用
const getList = () => {
  loading.value = true
  proxy.axios.get('/orders', {
    // params: {
    //   pageNum: pageNum.value
    // }
  }).then(res => {
    list.value = list.value.concat(res.list)
    loading.value = false
    if (res.hasNextPage) {
      busy.value = false
    } else {
      busy.value = true
    }
  })
}
const goPay = (orderNo) => {
  router.push({
    path: '/order/list',
    query: {
      orderNo
    }
  })
}
// 分页第一种方法：分页器
const handleChange = (pagenum) => {
  pageNum.value = pagenum
  getOrderList()
}
// 分页第二种方法：加载更多按钮
const loadMore = () => {
  pageNum.value++
  getOrderList()
}
// 分页第三种方法：滚动加载，通过插件实现
const scrollMore = () => {
  busy.value = true
  setTimeout(() => {
    pageNum.value++
    getList()
  }, 500)
}
</script>

<template>
  <div class="order-list">
    <order-header title="订单列表">
      <template #tip>
        <span>请谨防钓鱼链接或诈骗电话，了解更多></span>
      </template>
    </order-header>
    <div class="wrapper">
      <div class="container">
        <div class="order-box">
          <loading v-if="loading"></loading>
          <div class="order" v-for="(order, index) in list" :key="index">
            <div class="order-title">
              <div class="item-info fl">
                {{ order.createTime }}
                <span>|</span>
                {{ order.receiverName }}
                <span>|</span>
                订单号：{{ order.orderNo }}
                <span>|</span>
                {{ order.paymentTypeDesc }}
              </div>
              <div class="item-money fr">
                <span>应付金额：</span>
                <span class="money">{{ order.payment }}</span>
                <span>元</span>
              </div>
            </div>
            <div class="order-content clearfix">
              <div class="good-box fl">
                <div class="good-list" v-for="(item, i) in order.orderItemVoList" :key="i">
                  <div class="good-img">
                    <img v-lazy="prefix + item.productImage" alt="">
                  </div>
                  <div class="good-name">
                    <div class="p-name">{{ item.productName }}</div>
                    <div class="p-money">{{ item.totalPrice + 'X' + item.quantity }}元</div>
                  </div>
                </div>
              </div>
              <div class="good-state fr" v-if="order.status == 20">
                <a href="javascript:;">{{ order.statusDesc }}</a>
              </div>
              <div class="good-state fr" v-else>
                <a href="javascript:;" @click="goPay(order.orderNo)">{{ order.statusDesc }}</a>
              </div>
            </div>
          </div>
          <el-pagination background :pageSize="pageSize" layout="prev, pager, next" :total="total"
            @current-change="handleChange" class="pagination" v-if="false" />
          <div class="load-more" v-if="showNextPage">
            <el-button type="primary" :loading="loading" @click="loadMore">加载更多
            </el-button>
          </div>
          <div class="scroll-more" v-infinite-scroll="scrollMore" :infinite-scroll-disabled="busy"
            infinite-scroll-distance="410" infinite-scroll-immediate="true" v-if="true" style="overflow: hidden;">
            <img v-lazy="prefix + '/imgs/loading-svg/loading-spinning-bubbles.svg'" alt="" v-show="loading">
          </div>
          <no-data v-if="!loading && list.length == 0"></no-data>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
@import '@/assets/config.scss';
@import '@/assets/mixin.scss';

.order-list {
  .wrapper {
    background-color: $colorJ;
    padding-top: 33px;
    padding-bottom: 110px;

    .order-box {
      .order {
        @include border();
        background-color: $colorG;
        margin-bottom: 31px;

        &:last-child {
          margin-bottom: 0;
        }

        .order-title {
          @include height(74px);
          background-color: $colorK;
          padding: 0 43px;
          font-size: 16px;
          color: $colorC;

          .item-info {
            span {
              margin: 0 9px;
            }
          }

          .money {
            font-size: 26px;
            color: $colorB;
          }
        }

        .order-content {
          padding: 0 43px;

          .good-box {
            width: 88%;

            .good-list {
              display: flex;
              align-items: center;
              height: 145px;

              .good-img {
                width: 112px;

                img {
                  width: 100%;
                }
              }

              .good-name {
                font-size: 20px;
                color: $colorB;
              }
            }
          }

          .good-state {
            @include height(145px);
            font-size: 20px;
            color: $colorA;

            a {
              color: $colorA;
            }
          }
        }
      }

      .pagination {
        padding: 0 0 0 850px;
      }

      .el-pagination.is-background.pagination>ul>li.is-active.number {
        background-color: #FF6600;
      }

      .el-button--primary {
        background-color: #FF6600;
        border-color: #FF6600;
      }

      .load-more,
      .scroll-more {
        text-align: center;
      }
    }
  }
}
</style>